<template>
  <div style="padding:20px; border: 3px solid #d0d0d0; margin: 30px; border: 1px solid black;">
  <div class="block" style="padding:10px; ">
    <span class="demonstration">会员统计</span><br><br>
    <span class="demonstration">日期</span>
    <el-date-picker
      v-model="value2"
      type="datetimerange"
      :picker-options="pickerOptions"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      align="right"
      @blur="handleBlur">
    </el-date-picker>
    <span class="demonstration">会员类型</span>
    <el-select v-model="value" placeholder="请选择">
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
  </div>
    <div class="block" style="padding:10px; ">
      <el-table
        v-loading="loading"
        :data="tableData.slice((currentPage-1)*pageSize,currentPage*pageSize)"
        border
      >
        <el-table-column
          fixed
          prop="fileNumber"
          label="档案号"
          width="150"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          width="120"
        >
        </el-table-column>
        <el-table-column
          prop="sex"
          label="性别"
          width="120"
        >
        </el-table-column>
        <el-table-column
          prop="age"
          label="年龄"
          width="120"
        >
        </el-table-column>
        <el-table-column
          prop="username"
          label="健康管理师"
          width="300"
        >
        </el-table-column>
        <el-table-column
          prop="phoneNumber"
          label="手机号码"
          width="120"
        >
        </el-table-column>
        <el-table-column
          prop="regTime"
          label="注册时间"
          width="120"
        >
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data() {
    return {
      options: [{
        value: 'added',
        label: '新增会员'
      }, {
        value: 'reservation',
        label: '预约人数'
      }, {
        value: 'Upload',
        label: '报告上传'
      }],
      value: 'added',
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        },
          {
            text: '本年',
            onClick(picker) {
              const end = new Date();
              const start = new Date(new Date().getFullYear(), 0, 1);
              picker.$emit('pick', [start, end]);
            }
          }]
      },
      value1: [new Date(2000, 10, 10, 10, 10), new Date(2000, 10, 11, 10, 10)],
      value2: '',
      tableData: [],
      fileList: [],
      fromvo:[{
        added:"",
        reservation:"",
        upload:"",
        startDate:"",
        endDate:"",
      }],
      currentPage:1,
      pageSize:
        5,
      totalNum:
        10,
      currentPages:
        1,
    };
  },

  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.currentPage = val
      console.log(`当前页: ${val}`)
    },
    handleSizeChanges(val) {
      this.pageSizes = val
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChanges(val) {
      this.currentPages = val
      console.log(`当前页: ${val}`)
    },
    handleChange(file, fileList) {
      this.fileList = fileList.slice(-3);
    },

    handleBlur() {
      if(this.value2 === null || this.value2 === ''){
        return;
      }
      const dateStr = this.value2.toLocaleString(); // 获取日期字符串：2023/1/1 00:00:00,2023/9/23 11:33:36
// 使用逗号对日期字符串进行拆分
      const dates = dateStr.split(',');
      const startDateStr = dates[0].trim(); // 去除起始日期字符串前后的空格
      const endDateStr = dates[1].trim(); // 去除结束日期字符串前后的空格
      this.fromvo.startDate = startDateStr;
      this.fromvo.endDate = endDateStr;
console.log('handleBlur'+this.value2.toLocaleString());
console.log(this.value)
      this.fromvo.forEach(obj => {
        Object.keys(obj).forEach(key => {
          // 设置每个属性的值为空字符串
          obj[key] = '';
        });
      });
console.log(this.fromvo)
      this.fromvo.forEach((subtag, index) => {
        for (let key in subtag) {
          if (key === this.value) {
            console.log(`变量 a 是第 ${index+1} 个子标签对象的属性`);
            subtag[key] = 1; // 将属性的值更改为1
          }
        }
      });
console.log(this.fromvo)
// 处理光标消失事件的逻辑
      axios({
        method: 'post',
        url: 'api/member/statistics',
        headers: {
          'Content-Type': 'application/json'
        },
        data: {"added":this.fromvo.added,"reservation":this.fromvo.reservation,
          "upload":this.fromvo.upload,"startDate":this.fromvo.startDate,"endDate":this.fromvo.endDate}
      })
        .then(response => {
          console.log(response.data);
          this.tableData = response.data.data;
        })
        .catch(error => {
          console.error(error);
        });
    }

  }
};
</script>
<style>
.block {
  border: 1px solid #f0f0f0;
}
</style>
